package com.project.movie.controller.region;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.domain.MovieItem;
import com.project.movie.domain.MovieRegion;
import com.project.movie.domain.MovieType;
import com.project.movie.domain.Result;
import com.project.movie.service.MovieItemService;
import com.project.movie.service.MovieRegionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 国家/地区controller
 * @date 2025/02/06 08:51
 */
@Controller
@ResponseBody
@RequestMapping("region")
public class MovieRegionController {

    @Autowired
    private MovieRegionService movieRegionService;
    @Autowired
    private MovieItemService movieItemService;

    /** 分页获取国家/地区 */
    @PostMapping("getMovieRegionPage")
    public Result getMovieRegionPage(@RequestBody MovieRegion movieRegion) {
        Page<MovieRegion> page = new Page<>(movieRegion.getPageNumber(),movieRegion.getPageSize());
        QueryWrapper<MovieRegion> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(movieRegion.getName()),MovieRegion::getName,movieRegion.getName());
        Page<MovieRegion> movieRegionPage = movieRegionService.page(page, queryWrapper);
        return Result.success(movieRegionPage);
    }

    @GetMapping("getMovieRegionList")
    public Result getMovieRegionList() {
        List<MovieRegion> regionList = movieRegionService.list();
        return Result.success(regionList);
    }

    /** 根据id获取国家/地区 */
    @GetMapping("getMovieRegionById")
    public Result getMovieRegionById(@RequestParam("id")String id) {
        MovieRegion movieRegion = movieRegionService.getById(id);
        return Result.success(movieRegion);
    }

    /** 保存国家/地区 */
    @PostMapping("saveMovieRegion")
    public Result saveMovieRegion(@RequestBody MovieRegion movieRegion) {
        boolean save = movieRegionService.save(movieRegion);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑国家/地区 */
    @PostMapping("editMovieRegion")
    public Result editMovieRegion(@RequestBody MovieRegion movieRegion) {
        boolean save = movieRegionService.updateById(movieRegion);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除国家/地区 */
    @GetMapping("removeMovieRegion")
    public Result removeMovieRegion(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                MovieRegion region = movieRegionService.getById(id);
                QueryWrapper<MovieItem> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().like(MovieItem::getRegion,region.getName());
                int count = movieItemService.count(queryWrapper);
                if (count > 0) {
                    return Result.fail("区域下存在电影无法删除");
                }
                movieRegionService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("国家/地区id不能为空！");
        }
    }

}
